<?php

namespace addons\shop\controller;

use addons\epay\library\Service;
use addons\shop\model\Collect as CollectModel;
use think\Db;

class Inquiry extends Base
{
    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();
    }

    /* public function tableList($param)
     {

         $pageNum = 15;
         if (isset($param['num']) && !empty($param['num'])) {
             $pageNum = $param['num'];
         }

         return self::where(function ($query) use ($param) {


         })->order('createtime desc')->paginate($pageNum);
     }*/
    public function list()
    {
        $param = $this->request->param();
        $list  = \addons\shop\model\Inquiry::tableList($param);
        foreach ($list->items() as $item) {
            $item['imgs'] = explode(',', $item['imgs'])[0];
        }
        $train_one = Db::name("inquiry_problem")->limit(6)->order("createtime desc")->where('delete_time', null)->select();
        array_walk($train_one, function (&$item) {
            $item['imgs'] = explode(',', $item['imgs'])[0];
        });
        $train = Db::name("inquiry_problem")->order("createtime desc")->select();
        $this->view->assign('data_one', $train_one);
        //$this->view->assign('data', $train);
        $this->view->assign('data', $list);
        $this->view->assign('t_type', 2);
        $this->view->assign('inquiryList', $list);
        return $this->view->fetch('/inquiry/list');
    }

    public function list_qt()
    {
        $param        = $this->request->param();
        $param['uid'] = ['neq', $this->auth->id];
        $list         = \addons\shop\model\Inquiry::tableList($param);

        $this->view->assign('data', $list);
        $this->view->assign('t_type', 2);
        $this->view->assign('inquiryList', $list);
        return $this->view->fetch('/inquiry/list_qt');
    }

    /**
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 详情
     */
    public function detail()
    {
        $problem_id = $this->request->get('problem_id');
        if (!$problem_id) {
            $this->error("缺少必传参数");
        }

        $data           = Db::name("inquiry_problem i")
            ->field("i.*,u.username,u.avatar,u.nickname")
            ->join("user u", 'u.id=i.uid', 'left')
            ->where('i.id', $problem_id)
            ->find();
        $data['images'] = explode(',', $data['imgs']);

        return $this->fetch('/inquiry/detail', [
            'data'   => $data,
            't_type' => 2,
        ]);
    }

    public function inquiry()
    {
        $this->view->assign('t_type', 2);
        return $this->view->fetch('/inquiry/inquiry');
    }

    /**
     * @return void
     * 提现问题添加
     */
    public function inquiryadd()
    {
        $title = $this->request->post('title');
        $info  = $this->request->post('info');
        $imgs  = $this->request->post('imgs');
        if (!$title || !$info) {
            $this->error("缺少必传参数");
        }
        if (mb_strlen($title) > 20) {
            $this->error("标题过长");
        }
        $add_data['title']      = $title;
        $add_data['info']       = $info;
        $add_data['createtime'] = time();
        $add_data['updatetime'] = time();
        $add_data['imgs']       = $imgs;
        $add_data['uid']        = $this->auth->id ? $this->auth->id : 0;
        $in_data                = Db::name("inquiry_problem")->insert($add_data);
        if ($in_data) {
            $this->success('成功');
        } else {
            $this->error("失败");
        }
    }

    /**
     * @return void
     * 点赞数
     */
    public function fabulous()
    {
        $problem_id = $this->request->post('problem_id');
        if (!$problem_id) $this->error("缺少必传参数");
        $save1 = Db::name("inquiry_problem")->where("id", $problem_id)->setInc("fabulous_sum", 1);
        if ($save1) {
            $this->success('成功');
        } else {
            $this->error("失败");
        }
    }

    /**
     * @return void
     * 评论数
     */
    public function comment()
    {
        $user_id    = $this->auth->id;
        $problem_id = $this->request->post('problem_id');
        $comment    = $this->request->post('comment');
        if (!$problem_id || !$comment) $this->error("缺少必传参数");
        $save1                          = Db::name("inquiry_problem")->where("id", $problem_id)->setInc("comment_sum", 1);
        $add_data['user_id']            = $user_id;
        $add_data['inquiry_problem_id'] = $problem_id;
        $add_data['comment']            = $comment;
        $add_data['createtime']         = time();
        $add_data['updatetime']         = time();
        $add1                           = Db::name("inquiry_user_comment")->insert($add_data);
        if ($save1 && $add1) {
            $this->success('成功');
        } else {
            $this->error("失败");
        }
    }

    /**
     * @return void
     * 收藏数
     */
    public function collection()
    {
        $user_id    = $this->auth->id;
        $problem_id = $this->request->post('problem_id');
        if (!$problem_id) $this->error("缺少必传参数");
        $save1                          = Db::name("inquiry_problem")->where("id", $problem_id)->setInc("collection_sum", 1);
        $add_data['user_id']            = $user_id;
        $add_data['inquiry_problem_id'] = $problem_id;
        $add_data['createtime']         = time();
        $add_data['updatetime']         = time();
        $add1                           = Db::name("inquiry_user_collection")->insert($add_data);
        if ($save1 && $add1) {
            $this->success('成功');
        } else {
            $this->error("失败");
        }
    }

    /**
     * @return void
     * 打赏数
     */
    public function reward()
    {
        $user_id      = $this->auth->id;
        $problem_id   = $this->request->post('problem_id');
        $reward_money = $this->request->post('reward_money');
        $type         = $this->request->post('type', 'wechat');
        $method       = $this->request->post('method', 'wap');
        if (!$problem_id || !$reward_money) $this->error("缺少必传参数");
        /* $save1 = Db::name("info_article")->where("id",$article_id)->setInc("reward_sum",$reward_money);
         $add_data['user_id'] = $user_id;
         $add_data['info_article_id'] = $article_id;
         $add_data['money'] = $reward_money;
         $add_data['createtime'] = time();
         $add_data['updatetime'] = time();
         $add1 = Db::name("info_user_reward")->insert($add_data);
         if($save1 && $add1){
             $this->success('成功');
         }else{
             $this->error("失败");
         }*/
        //订单号
        $out_trade_no = date("YmdHis") . mt_rand(100000, 999999);
        //回调链接
        $notifyurl = $this->request->root(true) . '/addons/epay/index/inquiryreward/paytype/' . $type;
        $returnurl = $this->request->root(true) . '/addons/epay/index/returnx/paytype/' . $type . '/out_trade_no/' . $out_trade_no;

        $info = get_addon_info('third');
        if (!$info || !$info['state']) {
            $this->error("请在后台安装第三方登录插件");
        }
        try {
            $response = Service::submitOrder($reward_money, $out_trade_no, $type, "订单支付", $notifyurl, $returnurl, $method);
            //$response = Service::submitOrder($params);
            if ($response) {
                $add_data['user_id']            = $user_id;
                $add_data['inquiry_problem_id'] = $problem_id;
                $add_data['money']              = $reward_money;
                $add_data['status']             = 2;
                $add_data['order']              = $out_trade_no;
                $add_data['paytype']            = $type;
                $add_data['createtime']         = time();
                $add_data['updatetime']         = time();
                $add1                           = Db::name("inquiry_user_reward")->insert($add_data);
                if (!$add1) {
                    $this->error("数据有误");
                }
            }
        } catch (GatewayException $e) {
            throw new \Exception(config('app_debug') ? $e->getMessage() : "支付失败，请稍后重试");
        }
        //$response['order'] = $out_trade_no;
        $this->success('成功', $response);
    }

    /**
     * @return void
     * 购买
     */
    public function buy()
    {
        $user_id    = $this->auth->id;
        $problem_id = $this->request->post('problem_id');
        $type       = $this->request->post('type', 'wechat');
        $method     = $this->request->post('method', 'wap');
        if (!$problem_id) $this->error("缺少必传参数");
        //订单号
        $out_trade_no = date("YmdHis") . mt_rand(100000, 999999);
        //回调链接
        $notifyurl = $this->request->root(true) . '/addons/epay/index/inquirybuy/paytype/' . $type;
        $returnurl = $this->request->root(true) . '/addons/epay/index/returnx/paytype/' . $type . '/out_trade_no/' . $out_trade_no;

        $info = get_addon_info('third');
        if (!$info || !$info['state']) {
            $this->error("请在后台安装第三方登录插件");
        }
        $data  = Db::name("inquiry_problem")->where('id', $problem_id)->find();
        $money = $data['money'];
        try {
            $response = Service::submitOrder($money, $out_trade_no, $type, "订单支付", $notifyurl, $returnurl, $method);
            //$response = Service::submitOrder($params);
            if ($response) {
                $add_data['user_id']            = $user_id;
                $add_data['inquiry_problem_id'] = $problem_id;
                $add_data['money']              = $money;
                $add_data['status']             = 2;
                $add_data['order']              = $out_trade_no;
                $add_data['paytype']            = $type;
                $add_data['createtime']         = time();
                $add_data['updatetime']         = time();
                $add1                           = Db::name("inquiry_user_buy")->insert($add_data);
                if (!$add1) {
                    $this->error("数据有误");
                }
            }
        } catch (GatewayException $e) {
            throw new \Exception(config('app_debug') ? $e->getMessage() : "支付失败，请稍后重试");
        }
        //$response['order'] = $out_trade_no;
        $this->success('成功', $response);
    }

    /**
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 收藏
     */
    public function inquiry_collection()
    {
        $user_id        = $this->auth->id;
        $w['t.user_id'] = $user_id;
        $son_data       = Db::name("inquiry_user_collection t")->join('inquiry_problem ar', 't.inquiry_problem_id=ar.id')->where($w)->field("ar.*")->select();
        $this->success('成功', $son_data);
    }

    public function inquiry_comment()
    {
        $user_id        = $this->auth->id;
        $w['t.user_id'] = $user_id;
        $son_data       = Db::name("inquiry_user_comment t")->join('inquiry_problem ar', 't.inquiry_problem_id=ar.id')->where($w)->field("t.*,ar.title")->select();
        $this->success('成功', $son_data);
    }
}
